//
//  BankCardCell.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/10/8.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class BankCardCell: UITableViewCell {
    
    let bankImageView = UIImageView()
    let titleLabel = UILabel()
    let numberLabel = UILabel()
    let nameLabel = UILabel()
    
    var model : PackbackModel! {
        didSet{
           getData()
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.backgroundColor = UIColor.lightGray
        
        let colorView = UIView()
        colorView.layer.cornerRadius = 5
        colorView.layer.masksToBounds = true
        self.contentView.addSubview(colorView)
        
        let whiteView = UIView()
        whiteView.backgroundColor = UIColor.white
        whiteView.layer.cornerRadius = RATIO_H(maxNum: 15)
        whiteView.layer.masksToBounds = true
        colorView.addSubview(whiteView)
        
        whiteView.addSubview(bankImageView)
        
        titleLabel.text = BankofChina
        titleLabel.textColor = UIColor.white
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        colorView.addSubview(titleLabel)
        
        numberLabel.text = "8888888888"
        numberLabel.textColor = UIColor.white
        numberLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        colorView.addSubview(numberLabel)
        
        let personLabel = UILabel()
        personLabel.text = cardholder
        personLabel.textColor = UIColor.white
        personLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        colorView.addSubview(personLabel)
        
        nameLabel.textColor = UIColor.white
        nameLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        colorView.addSubview(nameLabel)
        
        colorView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 10))
            make.left.equalToSuperview().offset(RATIO(maxNum: 20))
            make.right.equalToSuperview().offset(RATIO(maxNum: -20))
            make.bottom.equalToSuperview().offset(RATIO_H(maxNum: -10))
        }
        
        whiteView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 20))
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 10))
            make.width.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        bankImageView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        numberLabel.snp.makeConstraints { (make) in
            make.top.equalTo(whiteView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.left.equalTo(whiteView)
            make.right.equalToSuperview().offset(RATIO(maxNum: -20))
            make.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        personLabel.snp.makeConstraints { (make) in
            make.top.equalTo(numberLabel.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.right.equalTo(numberLabel)
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.right.equalTo(personLabel)
        make.top.equalTo(personLabel.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        self.layoutIfNeeded()
        
        //颜色渐变
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [UIColor.endColor().cgColor,UIColor.centerColor().cgColor,UIColor.startBlue().cgColor]
        gradientLayer.locations = [0.3,0.5,1.0]
        gradientLayer.startPoint = .init(x: 0, y: 0)
        gradientLayer.endPoint = .init(x: 1.0, y: 0)
        gradientLayer.frame = .init(x: 0, y: 0, width: ScreenW-RATIO(maxNum: 40), height: RATIO_H(maxNum: 140))
        colorView.layer.insertSublayer(gradientLayer, at: 0)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }
    
    //MARK: 获取数据
    fileprivate func getData() {
        
        let image = KMTool.selectBankImage(bankName: model.bank ?? "")
        bankImageView.image = UIImage.imageWithName(image)
        titleLabel.text = model.bank
        numberLabel.text = model.card
        nameLabel.text = model.user
    }

}
